package com.five.wisdomEat.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.five.wisdomEat.entity.Dishes;
import com.five.wisdomEat.entity.DishesApplication;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DishesMapper extends BaseMapper<Dishes>{
    //查询商家所有菜品信息
    @Select("SELECT d.*,dt.dt_name,ds.ds_name,b.b_name,b.b_number,e.e_name FROM dishes d " +
            "INNER JOIN dishes_type dt ON d.d_typeId = dt.id " +
            "INNER JOIN dishes_state ds ON d.d_stateId = ds.id " +
            "INNER JOIN examine e ON d.d_examineId = e.id " +
            "INNER JOIN businessinfo b ON d.d_b_number = b.b_number WHERE b_number = #{bNumber}")
    List<Dishes> selectDishesList(String bNumber);

    //获取菜品数量
    @Select("SELECT COUNT(*) from dishes WHERE d_stateId = 1 AND d_examineId = 2")
    Integer selectDishesCount();

    //查询所有营业商家的菜品
    @Select("SELECT d.*,dt.dt_name,ds.ds_name,b.b_name,b.b_number,e.e_name FROM dishes d " +
            "INNER JOIN dishes_type dt ON d.d_typeId = dt.id " +
            "INNER JOIN dishes_state ds ON d.d_stateId = ds.id " +
            "INNER JOIN examine e ON d.d_examineId = e.id " +
            "INNER JOIN businessinfo b ON d.d_b_number = b.b_number " +
            "WHERE b_status = 1 AND d_stateId = 1 AND d_examineId = 2")
    List<Dishes> selectDishesOnSale();

    //通过商家编号查询菜品
    @Select("SELECT d.*,dt.dt_name,ds.ds_name,b.b_name,b.b_number,e.e_name FROM dishes d " +
            "INNER JOIN dishes_type dt ON d.d_typeId = dt.id " +
            "INNER JOIN dishes_state ds ON d.d_stateId = ds.id " +
            "INNER JOIN examine e ON d.d_examineId = e.id " +
            "INNER JOIN businessinfo b ON d.d_b_number = b.b_number WHERE d_number = #{dNumber}")
    List<Dishes> selectDishesByDNumber(String dNumber);

    //添加菜品
    @Insert("INSERT INTO dishes " +
            "(id,d_number,d_name,d_typeId,d_price,d_count,d_weight,d_photo,d_stateId,d_b_number,d_examineId) " +
            "VALUES (null,#{dNumber},#{dName},#{dTypeId},#{dPrice},#{dCount},#{dWeight},#{dPhoto},2,#{bNumber},1)")
    Integer insertDishes(Dishes dishes);

    //添加菜品申请
    @Insert("INSERT INTO dishes_application " +
            "(id,da_d_number,da_examineId,da_time) " +
            "VALUES (null,#{daDNumber},1,#{daTime})")
    Integer insertDishesApplication(DishesApplication dishesApplication);

    //根据菜品编号修改菜品信息
    @Update("UPDATE dishes SET d_name=#{dName},d_typeId=#{dTypeId},d_price=#{dPrice}, " +
            "d_count=#{dCount},d_weight=#{dWeight},d_photo=#{dPhoto} " +
            " WHERE d_number = #{dNumber}")
    Integer updateDishes(Dishes dishes);

    //上架菜品
    @Update("UPDATE dishes SET d_stateId = 1 WHERE d_number = #{dNumber}")
    Integer onSaleDishes(String dNumber);

    //下架菜品
    @Update("UPDATE dishes SET d_stateId = 2 WHERE d_number = #{dNumber}")
    Integer offShelfDishes(String dNumber);

    //根据菜品编号删除菜品
    @Delete("DELETE FROM dishes WHERE d_number = #{dNumber}")
    Integer deleteDishes(String dNumber);

    //不定查询
    @Select("SELECT d.*,dt.dt_name,ds.ds_name,b.b_name,e.e_name FROM dishes d " +
            "INNER JOIN dishes_type dt ON d.d_typeId = dt.id " +
            "INNER JOIN dishes_state ds ON d.d_stateId = ds.id " +
            "INNER JOIN businessinfo b ON d.d_b_number = b.b_number " +
            "INNER JOIN examine e ON d.d_examineId = e.id " +
            "WHERE d.d_b_number = #{bNumber} " +
            "AND d.d_number LIKE CONCAT('%', #{dNumber}, '%')  " +
            "AND d.d_name LIKE CONCAT('%', #{dName}, '%') " +
            "AND (d.d_stateId = #{dStateId} OR #{dStateId}<0) " +
            "AND (d.d_typeId = #{dTypeId} OR #{dTypeId}<0) ")
    List<Dishes> selectDishesListByExample(Dishes dishes);

    //不定查询营业商家的通过审批的上架菜品
    @Select("SELECT d.*,dt.dt_name,ds.ds_name,b.b_name FROM dishes d " +
            "INNER JOIN dishes_type dt ON d.d_typeId = dt.id " +
            "INNER JOIN dishes_state ds ON d.d_stateId = ds.id " +
            "INNER JOIN businessinfo b ON d.d_b_number = b.b_number " +
            "WHERE b_status = 1 AND d_stateId = 1 AND d_examineId = 2 " +
            "AND d.d_name LIKE CONCAT('%', #{dName}, '%') " +
            "AND b.b_name LIKE CONCAT('%', #{bName}, '%') " +
            "AND (d.d_typeId = #{dTypeId} OR #{dTypeId}<0) ")
    List<Dishes> selectOnSaleDishesByExample(Dishes dishes);

    //审核菜品是否重复
    @Select("SELECT COUNT(*) FROM dishes WHERE d_name = #{dName}")
    Integer checkDishes(String dName);

    //获取菜品申请列表
    @Select("SELECT  da.*,d.d_name,d.d_price,d.d_weight,d.d_photo,dt.dt_name,e.e_name,b.b_name,b.b_number " +
            "FROM dishes_application da " +
            "INNER JOIN dishes d ON da.da_d_number=d.d_number " +
            "INNER JOIN dishes_type dt ON d.d_typeId=dt.id " +
            "INNER JOIN examine e ON da.da_examineId = e.id " +
            "INNER JOIN businessinfo b ON d.d_b_number = b.b_number " +
            "ORDER BY da_time desc ")
    List<DishesApplication> selectDishesApplication();

    //同意菜品审核
    @Update("UPDATE dishes_application SET da_examineId = 2 WHERE id = #{id}")
    Integer applyDishesApplication(DishesApplication application);

    //修改菜品表审批状态
    @Update("UPDATE dishes SET d_examineId = 2 WHERE d_number = #{dNumber}")
    Integer applyDishes(String dNumber);


    //拒绝菜品审核
    @Update("UPDATE dishes_application SET da_examineId = 3 WHERE id = #{id}")
    Integer refuseDishesApplication(DishesApplication application);

    //修改菜品表审批状态
    @Update("UPDATE dishes SET d_examineId = 3 WHERE d_number = #{dNumber}")
    Integer refuseDishes(String dNumber);
}
